# -*- coding:utf-8 -*-
import random

import torndb

import setting
from util import gen_uuid
from util.date_util import add_months, curr_datetime_now

class GenFlightSeat(object):

    def __init__(self):

        self.db = torndb.Connection(
            host=setting.mysql_host,
            database=setting.mysql_database,
            user=setting.mysql_user,
            password=setting.mysql_password,
            time_zone="+8:00",
            charset='utf8mb4',
            max_idle_time=900,
            connect_timeout=900
        )

        self.date_list = ["2019-02-26", "2019-02-27", "2019-02-28", "2019-03-01", "2019-03-10", "2019-03-27", "2019-04-01"]

    def _get_flights(self):
        return self.db.query("select id from t_flight")

    def insert(self, flight_id, departure_date):
        due_time = add_months(curr_datetime_now(), 12)
        params = {"id": gen_uuid(), "flight_id": flight_id, "total_cnt": 10, "sellout_cnt": 0, "departure_date": departure_date}
        self.db.execute("insert into t_flight_seat (id, flight_id, total_cnt, departure_date, sellout_cnt, create_date, update_date) "
                        "values (%(id)s, %(flight_id)s, %(total_cnt)s, %(departure_date)s, %(sellout_cnt)s, now(), now())", **params)

    def gen(self):

        flights = self._get_flights()
        for flight in flights:
            for s in self.date_list:
                self.insert(flight.id, s)

if __name__ == '__main__':
    gen = GenFlightSeat()
    gen.gen()